package com.cmcc.waf.report.service;

import com.cmcc.waf.report.feign.OpenAPIFeign;
import com.cmcc.waf.report.feign.model.ResponseVO;
import com.cmcc.waf.report.feign.model.WebSafeInfoVO;
import com.cmcc.waf.report.feign.model.WebSafeRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

/**
 * @author waf
 */
@Service
public class AttackCountService {

    @Autowired
    private OpenAPIFeign feign;

    public ResponseVO<Object> getTotal(@RequestBody WebSafeRequest request) {
        Integer count = 0;
        ResponseVO<List<WebSafeInfoVO.AlarmTrend>> response = feign.getTotal(request);
        if (response.success()) {
            count = response.getBody().stream().mapToInt(WebSafeInfoVO.AlarmTrend::getCount).sum();
        }
        ResponseVO<Object> newResponseVO = new ResponseVO();
        newResponseVO.setCode(response.getCode());
        newResponseVO.setMessage(response.getMessage());
        newResponseVO.setBody(count);
        newResponseVO.setTraceId(response.getTraceId());
        return newResponseVO;
    }

    public ResponseVO<List<WebSafeInfoVO.AlarmTrend>> getCount(@RequestBody WebSafeRequest request) {
        return feign.getCount(request);
    }

}
